package quantri.chancePass;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpSession;

import dbutil.ConnectDB;

import utils.StringUtils;
import utils.UtilsOnScopes;

public class ChancePass {
	private String userName ="";
	private String oldPass ="";
	private String newPass ="";
	private String re_NewPass ="";
	private String error ="";

	public ChancePass() {
	}

	public ChancePass(String userName, String oldPass, String newPass,
			String re_NewPass, String error) {
		this.userName = userName;
		this.oldPass = oldPass;
		this.newPass = newPass;
		this.re_NewPass = re_NewPass;
		this.error = error;
	}

	public String getError() {
		return error;
	}

	public void setError(String error) {
		this.error = error;
	}

	public String getUserName() {
		return UtilsOnScopes.getUserName();
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getOldPass() {
		return oldPass;
	}

	public void setOldPass(String oldPass) {
		this.oldPass = oldPass;
	}

	public String getNewPass() {
		return newPass;
	}

	public void setNewPass(String newPass) {
		this.newPass = newPass;
	}

	public String getRe_NewPass() {
		return re_NewPass;
	}

	public void setRe_NewPass(String re_NewPass) {
		this.re_NewPass = re_NewPass;
	}
	/**************************************
	 * Method name		: chancePass
	 * Return type		: String
	 * Decription		:Doi mat khau
	 * Created date		: Jun 14, 2008
	 * Author			: Honglk
	 **************************************/
	public String chancePass(){
		String userName = UtilsOnScopes.getUserName();
		
		 if(this.checkPass() == false){
			return this.error;
		}else{
			if(this.newPass == null || this.newPass.equals("")){
				this.setError("Bạn chưa nhập mật khẩu mới !");
				return this.error;
			}
			else if(this.re_NewPass == null || this.re_NewPass.equals("")){
				this.setError("Bạn chưa nhập xác nhận mật khẩu !");
				return this.error;
			}
			else if(this.checkMatchPass() == false){
				return this.error;
			}
			else{
				try{
				ConnectDB.connectDB();
					String passInputEncrypted = StringUtils
							.encryptString(this.newPass);
					PreparedStatement preStat = ConnectDB.connect
							.prepareStatement("UPDATE thanhvien SET pass=? WHERE name='"
									+userName+"'");
					preStat.setString(1, passInputEncrypted);
					preStat.executeUpdate();
					this.setError("Đổi mật khẩu thành công !");
				} catch (SQLException e) {
					this.setError("Đổi mật khẩu không thành công !");
					e.printStackTrace();
				}
			}
		}
		return "";
	}
	
	/**************************************
	 * Method name		: checkMatchPass
	 * Return type		: boolean
	 * Decription		:kiem tra xem 2 mat khau co khop nhau khong?
	 * Created date		: Jun 14, 2008
	 * Author			: Honglk
	 **************************************/
	public boolean checkMatchPass(){
		boolean result = false;
		if(!(this.newPass.equals(this.re_NewPass))){
			this.setError("Mật khẩu không trùng nhau !");
			return result;
		}else{
			result = true;
		}
		return result;
	}
	
	/**************************************
	 * Method name		: checkPass
	 * Return type		: boolean
	 * Decription		:Kiem tra xem mat khau co dung la cua user do khong?
	 * Created date		: Jun 14, 2008
	 * Author			: Honglk
	 **************************************/
	public boolean checkPass() {
		boolean result = false;
		String userName = UtilsOnScopes.getUserName();
		String SQL = "SELECT A.pass FROM thanhvien A WHERE A.name='" + userName
				+ "'";
		ArrayList<HashMap<String, Object>> array = ConnectDB
				.getDataTypeList(SQL);
		HashMap<String, Object> hash = (HashMap<String, Object>) array.get(0);
		String pass = hash.get("pass").toString();
		String passInputEncrypted = StringUtils.encryptString(this.oldPass);
		if (!(pass.equals(passInputEncrypted))) {
			this.setError("Mật khẩu bạn vừa nhập không đúng !");
			return result;
		}else{
			result = true;
		}
		return result;
	}

}
